package frontend.components.other;

import backend.model.other.User;
import backend.service.other.UserService;
import frontend.core.TimeTrackingWebApplication;
import frontend.model.base.UIModel;
import frontend.components.base.ComponentFactory;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserSelectionPanel<UIM extends UIModel> extends Panel {

    public UserSelectionPanel(String id, UIM model, UserInputCallableAction<User> onSelectionAction, String labelResourceKey) {
        super(id);

        List<User> availableUsers = new ArrayList<User>();
        availableUsers.addAll(getAllActiveUsers());

        WebMarkupContainer userComponent = ComponentFactory.createDropDownChoiceComponent(
                "user", labelResourceKey, model, availableUsers,
                new DBModelChoiceRenderer<User>(), false, onSelectionAction);
        add(userComponent);
    }

    private List<User> getAllActiveUsers() {
        List<User> availableUsers = new ArrayList<User>();
        availableUsers.addAll(TimeTrackingWebApplication.getService(UserService.class).findAllActiveUsers());
        Collections.sort(availableUsers);
        return availableUsers;
    }
}
